package com.hyq.controller;

import com.hyq.utils.KafkaUtils;
import org.apache.kafka.clients.admin.NewTopic;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @author hanyq
 * @date 2024/6/18 17:21
 */
@RestController
public class KafkaController {

    @Resource
    private KafkaUtils kafkaUtils;

    @RequestMapping("lala")
    public void getTopics(){
        List<String> topicList = kafkaUtils.getTopicList();
        topicList.forEach(list -> {
            System.out.println(list);
        });
    }

    @RequestMapping("create")
    public void createTopics(String topic){
        NewTopic newTopic = new NewTopic("lala",3, (short) 1);
        kafkaUtils.createTopic(Collections.singleton(newTopic));
    }

    @RequestMapping("delete")
    public void deleteTopics(String topic){
        kafkaUtils.deleteTopic(Collections.singleton(topic));
    }

    @RequestMapping("getTopicByTopicName")
    public void getTopicByTopicName(String topic){
        String topicInfo = kafkaUtils.getTopicInfo(Collections.singleton(topic));
        System.out.println(topicInfo);
    }

    @RequestMapping("/sendMessage")
    public void sendMessage(@RequestParam("msg") String msg){
        kafkaUtils.sendMessageToTopic("lala","msg");
        kafkaUtils.sendMessageToTopic("hyq","msg");
    }
}
